package com.echat.companydata.controller;

import com.echat.companydata.service.FetchDataService;
import com.echat.companydata.tool.StringUtil;
import com.echat.companydata.vo.CompanyRecentData;
import com.echat.companydata.vo.StaffInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.*;

/**
 * Created by chenmohan on 2018/09/21
 *
 * @description
 */
@RestController
public class CompanyDataController {
    @Autowired
    FetchDataService fetchDataService;

    private static final Integer STAFF_ONLINE = 1;
    private static final Integer STAFF_BSUY = 2;
    private static final Integer STAFF_LEAVE = 3;
    private static final Integer STAFF_INVISIBLE = 4;

    /**
     * 请求指定公司ID的实时数据
     *
     * @param companyId
     * @throws IOException 公司ID：companyId
     *                     访客数量：visitorCount
     *                     对话数量：chatCount
     *                     在线客服数量：onlineStaffCount
     *                     忙碌客服数量：busyStaffCount
     *                     离开客服数量：offlineStaffCount
     *                     隐身客服数量：hiddenStaffCount
     */
    @RequestMapping("/companyRealtimeData")
    public Map getCompanyRealtimeData(Long companyId) {
        LinkedHashMap<String, Object> companyRealtimeDataMap = new LinkedHashMap<>();
        long visitorCount = fetchDataService.getCompanyVisitorOnLineNumber(companyId);
        long chatCount = fetchDataService.getCompanyTotalChatNumber(companyId);
        Map<Integer, Integer> companyStaffStatusNumber = fetchDataService.getStaffStatusMap(companyId);
        companyRealtimeDataMap.put("companyId", companyId);
        companyRealtimeDataMap.put("visitorCount", visitorCount);
        companyRealtimeDataMap.put("chatCount", chatCount);
        companyRealtimeDataMap.put("onlineStaffCount", companyStaffStatusNumber.get(STAFF_ONLINE));
        companyRealtimeDataMap.put("busyStaffCount", companyStaffStatusNumber.get(STAFF_BSUY));
        companyRealtimeDataMap.put("offlineStaffCount", companyStaffStatusNumber.get(STAFF_LEAVE));
        companyRealtimeDataMap.put("hiddenStaffCount", companyStaffStatusNumber.get(STAFF_INVISIBLE));
        return companyRealtimeDataMap;
    }

    /**
     * 请求指定公司ID的近7天数据
     *
     * @param companyId
     * @throws IOException 公司ID：companyId
     *                     List:
     *                     日期：dateTime
     *                     登录客服数量：loginStaffCount
     *                     访客数量：visitorCount
     *                     对话数量：chatCount
     *                     新访客数量：newVisitorCount
     */
    @RequestMapping("/companyDailyData")
    public Map getCompanyDataIn7Days(Long companyId) {
        LinkedHashMap<String, Object> resultMap = new LinkedHashMap<>();
        List<CompanyRecentData> res = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, 1);
        for (int i = 1; i <= 7; i++) {
            c.add(Calendar.DATE, -1);
            CompanyRecentData dataByDate = fetchDataService.getComanyDataByDate(companyId, c);
            res.add(dataByDate);
        }
        resultMap.put("companyId", companyId);
        resultMap.put("list", res);
        return resultMap;
    }

    @RequestMapping("/staffData")
    public Map getStaffData(Long companyId) {
        LinkedHashMap<String, Object> resultMap = new LinkedHashMap<>();
        List<Map> resultList = new ArrayList<>();
        List<StaffInfo> staffInfoList = fetchDataService.getStaffInfoList(companyId);
        for (StaffInfo staffInfo : staffInfoList) {
            LinkedHashMap<String, Object> staffInfoMap = new LinkedHashMap<>();
            staffInfoMap.put("staffName", staffInfo.getStaffName());
            staffInfoMap.put("staffNickName", StringUtil.isNotEmpty(staffInfo.getNickName()) ? staffInfo.getNickName() : staffInfo.getStaffName());
            staffInfoMap.put("loginName", staffInfo.getLoginName());
            staffInfoMap.put("staffRole", staffInfo.getRoleName());
            staffInfoMap.put("departmentList", fetchDataService.getDeparmentList(companyId, staffInfo.getDepartmentIds()));
            staffInfoMap.put("serviceAbility", staffInfo.getServiceAbility());
            staffInfoMap.put("autoChat", staffInfo.getAutoChat());
            staffInfoMap.put("autoOffline", staffInfo.getAutoOffline());
            resultList.add(staffInfoMap);
        }
        resultMap.put("companyId", companyId);
        resultMap.put("list", resultList);
        return resultMap;
    }

    @GetMapping("/ping")
    public String ping(){
        return "pong";
    }

    /*
    @Value("${timeout}")
    Integer timeout;

    @GetMapping("/properties")
    public Integer getTimeout(){
        System.err.println("读取到了apollo的配置: " + timeout);
        return timeout;
    }
    */
}
